package com.example.prodecttest.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prodecttest.dao.EquipmentTypeMapper;
import com.example.prodecttest.entity.EquipmentAttributeGroup;
import com.example.prodecttest.entity.EquipmentAttributeName;
import com.example.prodecttest.entity.EquipmentType;
import com.example.prodecttest.service.EquipmentTypeService;
import com.example.prodecttest.vo.EquipmentAttributeGroupVO;
import com.example.prodecttest.vo.EquipmentAttributeNameVO;
import com.example.prodecttest.vo.EquipmentTypeVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: fan
 * @date: 2022/2/18 13:54
 * @version: 1.0
 */
@Service
public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, EquipmentType> implements EquipmentTypeService {

    @Resource
    EquipmentAttributeGroupServiceImpl equipmentAttributeGroupService;

    @Resource
    EquipmentAttributeNameServiceImpl equipmentAttributeNameService;

    @Override
    public EquipmentTypeVO getEquipmentAttribute(Long equipmentTypeId) {


        EquipmentType equipmentType = getById(equipmentTypeId);
        if (ObjectUtils.isEmpty(equipmentType)) {
            throw new RuntimeException("没有对应的设备类型");
        }

        EquipmentTypeVO equipmentTypeVO = new EquipmentTypeVO();

        List<EquipmentAttributeGroup> equipmentAttributeGroupList =
                equipmentAttributeGroupService.getEquipmentAttributeGroupByEquipmentTypeId(equipmentTypeId);

        if (equipmentAttributeGroupList.isEmpty()) {
            return equipmentTypeVO;
        }

        List<EquipmentAttributeGroupVO> equipmentAttributeGroupVOList = new ArrayList<>(equipmentAttributeGroupList.size());

        equipmentAttributeGroupList.forEach(equipmentAttributeGroup -> {
            EquipmentAttributeGroupVO equipmentAttributeGroupVO = BeanUtil.toBean(equipmentAttributeGroup, EquipmentAttributeGroupVO.class);
            equipmentAttributeGroupVOList.add(equipmentAttributeGroupVO);

            List<EquipmentAttributeName> equipmentAttributeNameList =
                    equipmentAttributeNameService.listEquipmentAttributeNameByEquipmentGroupId(equipmentAttributeGroup.getId());

            if(equipmentAttributeNameList.isEmpty()){
                return;
            }

            equipmentAttributeGroupVO.setEquipmentAttributeNameVOList(equipmentAttributeNameList.stream()
                    .map(equipmentAttributeName -> BeanUtil.toBean(equipmentAttributeName, EquipmentAttributeNameVO.class))
                    .collect(Collectors.toList()));

        });

        equipmentTypeVO.setEquipmentAttributeGroupVOList(equipmentAttributeGroupVOList);

        return equipmentTypeVO;

    }
}
